package com.mubei.seckill.validator;

import com.mubei.seckill.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author 帅小伙
 * @date 2022/2/18
 * @description
 *
 * 手机格式校验器
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required = false;


    @Override
    public void initialize(IsMobile isMobile) {
        required = isMobile.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required) {
            return ValidatorUtil.IsMobile(s);
        }
        if(StringUtils.isEmpty(s)) {
            return true;
        }else{
            return ValidatorUtil.IsMobile(s);
        }
    }
}
